package com.milk.milkmedia.ui;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import com.j256.ormlite.dao.Dao;
import com.milk.milkmedia.R;
import com.milk.milkmedia.api.BaseActivity;
import com.milk.milkmedia.bean.MusicInfo;
import com.milk.milkmedia.dao.DBManager;
import com.milk.milkmedia.dao.MusicInfoDB;
import com.milk.milkmedia.service.MusicSearch;
import com.milk.milkmedia.utils.MusicListAdapter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 微安 on 2015/7/25.
 */
public class MusicListAcitvity extends BaseActivity implements View.OnClickListener{

    public static final int COMPLE = 1;
    private ListView mListView;
    private LinearLayout mLinearLayout;
    private Context mContext;
    private Dao<MusicInfoDB,String> iMusicInfoDB;
    public List<MusicInfo> iMusicInfos = new ArrayList<MusicInfo>();
    private MusicListAdapter iMusicListAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_musiclist);
        this.mContext = this;
        initView();
    }

    private void initView() {
        mLinearLayout = (LinearLayout) findViewById(R.id.mlist_search_ll);
        mLinearLayout.setVisibility(View.INVISIBLE);
        mListView = (ListView) findViewById(R.id.mlist_lv);
        loadMusicList();
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MusicListAcitvity.this,MusicPlayAcitivity.class);
                intent.putExtra("position",position);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mlist_search_ll:

                break;
            default:
                break;
        }
    }

    public class MsgHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case COMPLE:
                    updataUI();
                    break;
                default:
                    Toast.makeText(mContext,"未更新",Toast.LENGTH_SHORT);
                    break;
            }
        }
    }

    private void updataUI(){
    }

    /**
     * 从数据库中取出数据放过来
     */
    private void loadMusicList() {
        try {
            iMusicInfoDB = DBManager.getInstance().getMusicInfoBDHelper().getDao(MusicInfoDB.class);
            int Count = readDataFromDataBase();
            if (Count == 0){
                mLinearLayout.setVisibility(View.VISIBLE);
            }else{
                mLinearLayout.setVisibility(View.INVISIBLE);
            }
            iMusicListAdapter = new MusicListAdapter(mContext,iMusicInfos);
            mListView.setAdapter(iMusicListAdapter);
        } catch (SQLException e) {
            e.printStackTrace();
            Log.e(HomePageActivity.Tag, "MusicListAdapter建立iMusicInfoDao错误");
        }
    }

    class ServiceTask extends AsyncTask<String,Integer,Integer>{

        @Override
        protected void onPostExecute(Integer integer) {
            super.onPostExecute(integer);
            loadMusicList();
        }


        @Override
        protected Integer doInBackground(String... params) {
            MusicSearch musicSearch = new MusicSearch();
            musicSearch.run();
            return null;
        }


        @Override
        protected void onProgressUpdate(Integer... values)
        {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }
    }
    ;

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.menu_search_music:
                ServiceTask serviceTask = new ServiceTask();
                serviceTask.execute("20");
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_musiclist,menu);
        return true;
    }

    private String getSortKey(String sortKey){
        String key = sortKey.substring(0,1).toUpperCase();
        if (key.matches("[A-Z]]")){
            return key;
        }
        return "#";
    }

    private int readDataFromDataBase(){
        List<MusicInfoDB> musicInfoDBList = new ArrayList<MusicInfoDB>();
        try {
            musicInfoDBList = iMusicInfoDB.queryForAll();
            MusicInfo musicInfo;
            for (int i = 0;i< musicInfoDBList.size();i++)
            {
                int id = musicInfoDBList.get(i).getId();
                String title = musicInfoDBList.get(i).getTitle();
                String url = musicInfoDBList.get(i).getUrl();
                long size = musicInfoDBList.get(i).getSize();
                musicInfo = new MusicInfo(title,url,size);
                iMusicInfos.add(musicInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            Log.e(HomePageActivity.Tag, "读取musicInfo数据库错误");
        }
        return musicInfoDBList.size();
    }

}
